package com.sskj.gocoin.v.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextSwitcher;
import android.widget.TextView;

import com.sskj.gocoin.MyAppLication;
import com.sskj.gocoin.R;
import com.sskj.gocoin.common.GlideImageLoader;
import com.sskj.gocoin.utils.ClickUtil;
import com.sskj.gocoin.v.activity.BannerWebActivity;
import com.sskj.gocoin.v.activity.InforMationActivity;
import com.sskj.gocoin.v.activity.LoginActivity;
import com.sskj.gocoin.v.activity.MainActivity;
import com.sskj.gocoin.v.activity.WebViewActivity;
import com.sskj.gocoin.v.base.BaseFragment;
import com.youth.banner.Banner;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.disposables.Disposable;

/**
 * Created by Lee on 2018/2/28 0028.
 */

public class MarketFragment extends BaseFragment<MainActivity, MarketFragmentPresenter> {
    @BindView(R.id.banner)
    Banner banner;// Banner图
    @BindView(R.id.tvNotice)
    TextSwitcher tvNotice;// 信息通知栏
    @BindView(R.id.notice_more)
    TextView noticeMore;

    //    @BindView(R.id.market_list)
//    ViewPager marketList;

    private MarketListFragment marketListFragment;
    private List<com.sskj.gocoin.m.bean.bean.Banner.BannerList> bannerList;
    private boolean isBannerShow = false;
    private ArrayList<String> images;
    private com.sskj.gocoin.m.bean.bean.Banner.ArticleList noticeList;
    private Disposable noticeDispo;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_market;
    }

    @Override
    public MarketFragmentPresenter getPresenter() {
        return new MarketFragmentPresenter();
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        marketListFragment = MarketListFragment.newInstance(0);
        FragmentTransaction ft = getChildFragmentManager().beginTransaction();
        ft.add(R.id.market_list, marketListFragment);
        ft.show(marketListFragment);
        ft.commitNow();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (marketListFragment != null) {
            if (!hidden) {
//                RxBus.getDefault().send(1);
                marketListFragment.update();
                marketListFragment.openWebSocket();
            }
//            else {
//                marketListFragment.closeWebSocket();
//            }
        }
    }


    public void openWebSocket() {
        if (!isHidden()) {
            if (marketListFragment != null) {
                marketListFragment.openWebSocket();
            }
        }
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    public void closeWebSocket() {
        if (marketListFragment != null) {
            marketListFragment.closeWebSocket();
        }
    }

    @Override
    public void initView() {
        //设置图片加载器
        banner.setImageLoader(new GlideImageLoader());
        //
        banner.setOnBannerListener(position -> {
            if (!TextUtils.isEmpty(bannerList.get(position).getUrl())) {
                BannerWebActivity.start(getActivity(), bannerList.get(position).getUrl());
            }
        });

        //设置图片集合
        images = new ArrayList<>();

        tvNotice.setFactory(() -> {
            TextView textView = new TextView(getActivity());
            textView.setTextColor(getResources().getColor(R.color.market_article));
            return textView;
        });

        ClickUtil.click(tvNotice, () -> {
            if (noticeList != null) {
                WebViewActivity.start(getActivity(), noticeList.getId());
            }
        });

        ClickUtil.click(noticeMore, () -> {
            if (MyAppLication.isLogin()) {
                startActivity(new Intent(mActivity, InforMationActivity.class));
            } else {
                startActivity(new Intent(mActivity, LoginActivity.class));
            }
        });

    }

    @Override
    public void initData() {
        mPresenter.getBannerUrl();
    }

    /**
     * 接口回调，显示Banner图
     */
    public void showBanner(com.sskj.gocoin.m.bean.bean.Banner bannerIamge) {
        bannerList = bannerIamge.getBanner();  // 获取Banner图地址
        images.clear();
        for (int i = 0; i < bannerList.size(); i++) {
            images.add(bannerList.get(i).getBanner_url());
        }
        if (this.banner == null)
            return;
        this.banner.setImages(images);
        //banner设置方法全部调用完毕时最后调用
        this.banner.start();

        List<com.sskj.gocoin.m.bean.bean.Banner.ArticleList> articleList = bannerIamge.getArticle();  // 广播信息
        setNotice(articleList);
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser && banner != null && !isBannerShow && mPresenter != null) {
            mPresenter.getBannerUrl();
        }
    }

    public void setNotice(List<com.sskj.gocoin.m.bean.bean.Banner.ArticleList> notice) {
        if (tvNotice != null&&notice.size()>0) {
            noticeList = notice.get(0);
            tvNotice.setText(notice.get(0).getTitle());
//            DisposUtil.close(noticeDispo);
//            noticeDispo = Flowable.interval(0, 5, TimeUnit.SECONDS)
//                    .onBackpressureDrop()
//                    .subscribeOn(Schedulers.newThread())
//                    .observeOn(AndroidSchedulers.mainThread())
//                    .subscribe(i -> {
//                        if (tvNotice != null) {
//                            noticeList = notice.get((int) (i % notice.size()));
//                            tvNotice.setText(noticeList.getTitle());
//                        }
//                    }, System.out::println);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
//        DisposUtil.close(noticeDispo);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

}
